//
//  MainViewController.m
//  Gunz
//
//  Created by Joggy on 15/12/12.
//  Copyright © 2015年 NUPT. All rights reserved.
//

#import "MainViewController.h"
#import "ChatListViewController.h"
#import "QYNotice.h"
#import "TaskDetailViewController.h"
#import "MJRefresh.h"
#import "GZNetRequest.h"

//    NSURL *url=[NSURL URLWithString:@"http://180.209.64.49:8002/user/verified"];

@implementation MainViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
    tb.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    tb.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    [tb.mj_header beginRefreshing];
//    UIImage *image1 = [UIImage imageNamed:@"插图3"];
//    UIImage *image2 = [UIImage imageNamed:@"插图1"];
//    NSDictionary *para = @{@"isreal": @"id1.jpg", @"photoback": @"id2.jpg", @"idnum": @"412702"};

//    AFHTTPSessionManager *manager = [GZNetRequest getPOSTManager];
//    NSURLSessionDataTask *tesk = [manager POST:@"http://180.209.64.49:8001/user/verified" parameters:para constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        [formData appendPartWithFileData:UIImagePNGRepresentation(image1) name:@"file" fileName:@"id1.jpg" mimeType:@"image/jpeg"];
//        [formData appendPartWithFileData:UIImagePNGRepresentation(image2) name:@"file" fileName:@"id2.jpg" mimeType:@"image/jpeg"];
//    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@", responseObject);
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@", error);
//    }];
//    [tesk resume];
}

- (void)prepareForNav{
    [super prepareForNav];
    self.title = @"首页";
    //导航左按键
    locaView = [[MLocationView alloc] init];
    [locaView.locationButton addTarget:self action:@selector(selectLocation) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:locaView];
    //导航右按键
    UIImage *rightImage = [UIImage imageNamed:@"main_sms"];
    rightBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBarButton.frame = CGRectMake(0, 0, rightImage.size.width, rightImage.size.height);
    rightBarButton.layer.position = CGPointMake(screenWidth - 22, 22);
    [rightBarButton addTarget:self action:@selector(chatList) forControlEvents:UIControlEventTouchUpInside];
    [rightBarButton setImage:[rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    //列表轮播图
    lb = [[LBView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, MVLbHeight)];
    [lb scrollImagesByNet:@[@"http://pic.58pic.com/58pic/17/32/69/25e58PICyAR_1024.jpg", @"http://img11.360buyimg.com/cms/jfs/t196/241/2413424678/109726/34738058/53cf6e32Na0cbe3e5.jpg", @"http://pic41.nipic.com/20140511/18494287_234604352196_2.jpg", @"http://pic.58pic.com/58pic/13/40/44/79U58PICqtW_1024.jpg"] placeHolderImage:[UIImage imageNamed:@"lunbo_main"]];
    [lb scroll];
    //列表
    tb = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight - 64) style:UITableViewStylePlain];
    tb.delegate = self;
    tb.dataSource = self;
    tb.backgroundColor = [UIColor clearColor];
    tb.tableHeaderView = lb;
    tb.showsVerticalScrollIndicator = false;
    tb.showsHorizontalScrollIndicator = false;
    tb.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tb registerClass:[MVTableViewCell class] forCellReuseIdentifier:@"reusecell"];
    [self.view addSubview:tb];
    locationPikcer = [[QYLocationPickerView alloc] init];
    [locationPikcer.selectButton addTarget:self action:@selector(updateLocation) forControlEvents:UIControlEventTouchUpInside];
}

#pragma 事件方法

- (void)selectLocation {
    [locationPikcer show];
}

- (void)updateLocation {
    [locationPikcer dismiss];
    [locaView.locationButton setAttributedTitle:[[NSAttributedString alloc] initWithString:locationPikcer.city attributes:@{NSFontAttributeName: [UIFont systemFontOfSize: font14], NSForegroundColorAttributeName: CUSTOM_GREEN, NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]}] forState:UIControlStateNormal];
}

- (void)headerRefresh {
    NSDictionary *para = @{@"lastid": @"0"};
    __weak typeof (self) weakSelf = self;
    [[GZNetRequest getPOSTManager] POST:@"http://180.209.64.49:8001/task/getListById" parameters:para progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic1 = responseObject;
        NSLog(@"%@", responseObject);
        data = dic1[@"msg"];
        if(data.count <= 0) {
            data = [weakSelf defaultData];
            [tb reloadData];
            [tb.mj_header endRefreshing];
        }
        NSLog(@"%@", data);
        [tb reloadData];
        [tb.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        data = [weakSelf defaultData];
        [tb reloadData];
        [tb.mj_header endRefreshing];
    }];
}

- (void)footerRefresh {
    [tb.mj_footer endRefreshing];
}

- (void)chatList {
    ChatListViewController *chatList = [[ChatListViewController alloc] init];
    chatList.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:chatList animated:true];
}

#pragma 代理方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    NSDictionary *tep = data[indexPath.section];
    if([tep[@"icontask"] isEqualToString:@"__no"]) {
        TaskDetailViewController *detail = [[TaskDetailViewController alloc] init];
        detail.isMineRelease = false;
        detail.hidesBottomBarWhenPushed = true;
        detail.data = tep[@"fields"];
        [self.navigationController pushViewController:detail animated:true];
    }
    else {
        NSDictionary *para = @{@"id": tep[@"id"], @"parenttask_id": tep[@"parenttask_id"]};
        [[GZNetRequest getPOSTManager] POST:@"http://180.209.64.49:8001/task/taskDetail" parameters:para progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dic1 = responseObject;
            NSArray *arr1 = dic1[@"msg"];
            NSDictionary *dic2 = arr1[0];
            NSDictionary *dic3 = dic2[@"fields"];
            TaskDetailViewController *detail = [[TaskDetailViewController alloc] init];
            detail.isMineRelease = false;
            detail.hidesBottomBarWhenPushed = true;
            detail.data = dic3;
            [self.navigationController pushViewController:detail animated:true];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return data.count;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, MVCInsert)];
    headView.backgroundColor = [UIColor clearColor];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return MVCInsert;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return MVCCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MVTableViewCell *cell = (MVTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"reusecell"];
    NSDictionary *dic = data[indexPath.section];
    NSString *imgURL = @"http://180.209.64.49:8001/";
    imgURL = [imgURL stringByAppendingString:dic[@"icontask"]];
    [cell.headImage sd_setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:[UIImage imageNamed:@"插图1"]];
    cell.cellTitle.text = dic[@"name"];
    NSString *detailStr = dic[@"describe"];
    cell.detail.text = detailStr.description;
    NSString *priceStr = dic[@"faretask"];
    priceStr = [priceStr.description stringByAppendingString:@"¥"];
    cell.priceLabel.text = priceStr;
    return cell;
}

- (NSArray*)defaultData {
    
    NSArray* array = @[
                       @{
                           @"icontask": @"__no",
                           @"name": @"南京本地话语音采集",
                           @"describe": @"采集南京本地话",
                           @"faretask": @"16",
                           @"fields": @{
                                   @"icontask": @"no",
                                   @"name": @"南京本地话语音采集",
                                   @"faretask": @"16",
                                   @"ownuser": @"简果公司",
                                   @"starttime": @"2016",
                                   @"overtime": @"2016",
                                   @"describe": @"采集南京本地话",
                                   @"templete": @"无"
                                   }
                           },
                       @{
                           @"icontask": @"__no",
                           @"name": @"南京本地话语音采集",
                           @"describe": @"采集南京本地话",
                           @"faretask": @"16",
                           @"fields": @{
                                   @"icontask": @"no",
                                   @"name": @"南京本地话语音采集",
                                   @"faretask": @"16",
                                   @"ownuser": @"简果公司",
                                   @"starttime": @"2016",
                                   @"overtime": @"2016",
                                   @"describe": @"采集南京本地话",
                                   @"templete": @"无"
                                   }
                           },
                       @{
                           @"icontask": @"__no",
                           @"name": @"南京本地话语音采集",
                           @"describe": @"采集南京本地话",
                           @"faretask": @"16",
                           @"fields": @{
                                   @"icontask": @"no",
                                   @"name": @"南京本地话语音采集",
                                   @"faretask": @"16",
                                   @"ownuser": @"简果公司",
                                   @"starttime": @"2016",
                                   @"overtime": @"2016",
                                   @"describe": @"采集南京本地话",
                                   @"templete": @"无"
                                   }
                           }
                       ];
    
    return array;
}

@end



